package part1.solution9;

/**
 * @Author: shaochong
 * @Date: 2021/8/8
 * @Description:
 */
public class Solution9 {
    public boolean isPalindrome(int x) {
        String xx = x + "";
        int len = xx.length();

        for (int i = 0; i< len / 2 + 1; i++) {
            if (xx.charAt(i) != xx.charAt(len-1-i)) {
                return false;
            }
        }
        return true;
    }


    public static boolean isPalindrome2(int x) {
        // 得到数字的位数
        if(x<0) return false;
        int n = 0;
        int temp = x;
        while (temp > 0) {
            temp = temp / 10;
            n++;
        }

        int mid = n / 2;
        int sum = 0;
        int extra = x;

        for(int i = 0; i <  mid; i++) {
            int gewei = extra % 10;
            extra = extra / 10;
            sum = sum * 10 + gewei;
        }

        if (n % 2 != 0) {
            extra = extra / 10;
        }
        System.out.println(String.format("sum=%s,extra=%s", sum, extra));
        if (sum == extra) {
            return true;
        }
        return false;

    }


    public static boolean isPalindrome3(int x) {
        // 得到数字的位数
        if(x<0) return false;
        if (x == 0) return true;
        if (x % 10 == 2) return false;
        int sum = 0;
        while(x > sum) {
            sum = sum * 10 + x % 10;
            x /= 10;
        }
        System.out.println(String.format("sum=%s,extra=%s", sum, x));
        return x == sum || x == sum / 10;

    }



    public static void main(String[] args) {
        System.out.println(isPalindrome3(10));
    }


}
